﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MeizuSDK.Presentation;
using System.Drawing;
using MeizuSDK.Core;
using System.Threading;
using MeizuSDK.Drawing;

namespace Demo
{
    class BasicControlWindow : DemoWindowBase
    {
        ScrollableControl scrollPanel;

        TextBox textbox = new TextBox();
        Button btnFlyMode = new Button();
        Label label = new Label();
        Switch swiBluetook = new Switch();
        MultilineTextBox multilineTextBox = new MultilineTextBox();
        Button dateButton = new Button();
        GradientButton btnVibrate = new GradientButton();
        Button btnHardwareVersion = new Button();

        public BasicControlWindow()
        {
            AnimationIn = AnimationType.ScrollRightToLeftPush;
        }

        protected override void OnLoad(EventArgs e)
        {
            toolbar.SetTextButton(ToolBarButtonIndex.MiddleTextButton, true, true, "隐藏输入法");
            toolbar.SetTextButton(ToolBarButtonIndex.RightTextButton, true, true, "xxx");

            scrollPanel = new ScrollableControl();
            scrollPanel.Location = new Point(0, 0);
            scrollPanel.Size = new Size(Width, Height - ToolBar.HEIGHT);

            Controls.Add(scrollPanel);

            textbox.Location = new Point(10, 10);
            textbox.Text = "文字 test";

            multilineTextBox.Location = new Point(10, 80);
            multilineTextBox.Tip = "俺是多行文本框";
            multilineTextBox.Size = new Size(460, 100);
            multilineTextBox.EnableMagnifier = false;// 启用放大镜（注意：启用放大镜会使应用程序内存占用大于10MB，而且无法释放，因此建议不启用放大镜）
            multilineTextBox.SetSipMode(SipMode.DigitEx);

            btnFlyMode.Text = "飞行模式";
            btnFlyMode.Location = new Point(10, 180);
            btnFlyMode.ButtonType = Button.Type.Green;
            btnFlyMode.Click += new EventHandler(btnFlyMode_Click);

            swiBluetook.Location = new Point(10, 280);
            swiBluetook.StateChanged += new EventHandler<Button.StateEventArgs>(swiBluetooth_StateChanged);

            btnVibrate.Text = "马达震动";
            btnVibrate.Location = new Point(10, 380);
            btnVibrate.Size = new Size(Button.WIDTH, Button.HEIGHT);
            btnVibrate.Click += new EventHandler(gradientButton_Click);

            label.Text = "我是Label";
            label.Size = new Size(200, Label.HEIGHT);
            label.Location = new Point(250, 180);
            label.ForeColor = Color.Red;

            dateButton.Text = DateTime.Now.ToString("yyyy年MM月dd日");
            dateButton.Location = new Point(250, 280);
            dateButton.Click += new EventHandler(dateButton_Click);

            btnHardwareVersion.Text = "硬件版本号";
            btnHardwareVersion.Location = new Point(250, 380);
            btnHardwareVersion.Click += new EventHandler(btnHardwareVersion_Click);

            scrollPanel.Controls.Add(textbox);
            scrollPanel.Controls.Add(btnFlyMode);
            scrollPanel.Controls.Add(label);
            scrollPanel.Controls.Add(swiBluetook);
            scrollPanel.Controls.Add(dateButton);
            scrollPanel.Controls.Add(multilineTextBox);
            scrollPanel.Controls.Add(btnVibrate);
            scrollPanel.Controls.Add(btnHardwareVersion);

            base.OnLoad(e);
        }

        void btnHardwareVersion_Click(object sender, EventArgs e)
        {
            Toast.Show("HdVer：" +DeviceEnv.HardwareVersion.ToString());
        }

        void gradientButton_Click(object sender, EventArgs e)
        {
            MeizuSDK.Core.MotorVibrateAPI.SetVibrateOn(3000, 1000);
        }

        void dateButton_Click(object sender, EventArgs e)
        {
            SetDailyDialog sdd = new SetDailyDialog();
            sdd.AnimateIn = AnimationType.ZoomIn;
            sdd.AnimateOut = AnimationType.Fade;

            sdd.Setting.ItemCount = 3;
            sdd.Setting.Item1Max = 2100 - 1923;
            sdd.Setting.Item2Max = 12;
            sdd.Setting.Item3Max = sdd.MaxDaysInMonth(DateTime.Now.Month, DateTime.Now.Year);

            // 设置对话框初始显示日期的项index，index值从0开始 
            sdd.Setting.Item1Index = DateTime.Now.Year - 1923;
            sdd.Setting.Item2Index = DateTime.Now.Month - 1;
            sdd.Setting.Item3Index = DateTime.Now.Day - 1;

            sdd.Setting.Item1Text = "年";
            sdd.Setting.Item2Text = "月";
            sdd.Setting.Item3Text = "日";

            sdd.Setting.Item1Width = sdd.Setting.Item2Width = sdd.Setting.Item3Width = 140;

            sdd.DisplayYear = true;
            sdd.EnableChangeItemValue = true;


            if (sdd.ShowDialog(HWnd) == System.Windows.Forms.DialogResult.OK)
            {
                DateTime dt = new DateTime(sdd.SelectedItem1Value, sdd.SelectedItem2Value, sdd.SelectedItem3Value);

                dateButton.Text = dt.ToString("yyyy年MM月dd日");
            }
        }

        void swiBluetooth_StateChanged(object sender, Button.StateEventArgs e)
        {
            if (e.State == Button.State.On)
            {
                if(Bluetooth.OpenBluetooth())
                {
                    MessageBox.Show("打开成功！蓝牙地址：" + Bluetooth.Address);
                }
                else
                {
                    MessageBox.Show("打开失败！");
                }
            }
            else
            {
                Bluetooth.CloseBluetooth();
            }
        }

        void btnFlyMode_Click(object sender, EventArgs e)
        {
            int index = PopupMenu.Show(true, "开启飞行模式", "关闭飞行模式");

            switch (index)
            {
                case 0:
                    FlyModeSetting.SetFlyMode(this.HWnd, true);
                    break;
                case 1:
                    FlyModeSetting.SetFlyMode(this.HWnd, false);
                    break;
            }

            MessageBox.Show("设置完成！", "提示");
        }

        protected override void toolbar_ButtonClick(object sender, ToolBar.ButtonEventArgs e)
        {
            switch (e.Index)
            {
                case ToolBarButtonIndex.MiddleTextButton:
                    if (SipHelper.IsSipOpen)
                    {
                        SipHelper.CloseSip();
                    }
                    break;
                case ToolBarButtonIndex.RightTextButton:
                    {

                    }
                    break;
                default:
                    base.toolbar_ButtonClick(sender, e);
                    break;
            }
        }

        protected override void Dispose(bool disposing)
        {
            // 关闭马达
            MeizuSDK.Core.MotorVibrateAPI.SetVibrateOff();

            base.Dispose(disposing);
        }
    }
}
